package com.cyb.myfriend.controller;

import com.cyb.myfriend.constant.BaseResponse;
import com.cyb.myfriend.constant.StatusCode;
import com.cyb.myfriend.constant.UserConstant;
import com.cyb.myfriend.domain.dto.*;
import com.cyb.myfriend.domain.entity.User;
import com.cyb.myfriend.domain.vo.UserLoginVO;
import com.cyb.myfriend.exception.BusinessException;
import com.cyb.myfriend.service.UserService;
import com.cyb.myfriend.utils.ResultUtil;

import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户控制层
 *
 * @author cyb
 * @date 2024/08/10 09:10
 **/
@RestController
@RequestMapping("/user")
@Api("用户控制层")
public class UserController {

    @Resource
    private UserService userService;
    @ApiOperation("用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserRegisterDTO", value = "用户注册DTO")
    })
    @PostMapping("/register")
    public BaseResponse<Integer> register(@RequestBody UserRegisterDTO user) {
        if (user == null) {
            throw new BusinessException(StatusCode.NULL_PARAMS);
        }
        String userAccount = user.getUserAccount();
        String userPassword = user.getUserPassword();
        String exPassword = user.getExPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, exPassword)) {
            throw new BusinessException(StatusCode.NULL_PARAMS, "参数不能为空");
        }
        int result = userService.register(userAccount, userPassword, exPassword);
        return ResultUtil.success(result, "注册成功");
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseResponse<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request) {
        if (userLoginDTO == null) {
            throw new BusinessException(StatusCode.NULL_PARAMS);
        }
        String userAccount = userLoginDTO.getUserAccount();
        String userPassword = userLoginDTO.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            return ResultUtil.fail(StatusCode.NULL_PARAMS.getCode(), "参数为空");
        }
        User user = userService.login(userAccount, userPassword, request);
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(user, userLoginVO);
        return ResultUtil.success(userLoginVO, "登录成功！");
    }

    @ApiOperation("添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserSaveDTO", value = "添加用户DTO"),
            @ApiImplicitParam(name = "HttpServletRequest", value = "Http请求")
    })
    @PostMapping("/save")
    public BaseResponse<Integer> saveUser(@RequestBody UserSaveDTO userSaveDTO, HttpServletRequest request) {
        if (userSaveDTO == null) {
            throw new BusinessException(StatusCode.NULL_PARAMS);
        }
        User user = new User();
        BeanUtils.copyProperties(userSaveDTO, user);
        int result = userService.saveUser(user, request);
        return ResultUtil.success(result, "添加用户成功！");
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserDeleteDTO", value = "删除用户DTO"),
            @ApiImplicitParam(name = "HttpServletRequest", value = "Http请求")
    })
    @PostMapping("/delete")
    public BaseResponse<Integer> deleteUser(@RequestBody UserDeleteDTO userDeleteDTO, HttpServletRequest request) {
        if (userDeleteDTO == null) {
            throw new BusinessException(StatusCode.NULL_PARAMS);
        }
        User user = new User();
        BeanUtils.copyProperties(userDeleteDTO, user);
        int result = userService.deleteUser(user, request);
        return ResultUtil.success(result, "删除用户成功");
    }

    @ApiOperation("更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "UserSaveDTO", value = "更新用户DTO"),
            @ApiImplicitParam(name = "HttpServletRequest", value = "Http请求")
    })
    @PostMapping("/update")
    public BaseResponse<Integer> updateUser(@RequestBody UserSaveDTO userSaveDTO, HttpServletRequest request) {
        if (userSaveDTO == null) {
            throw new BusinessException(StatusCode.NULL_PARAMS);
        }
        User user = new User();
        BeanUtils.copyProperties(userSaveDTO, user);
        int result = userService.updateUser(user, request);
        return ResultUtil.success(result, "更新用户成功");
    }

    @ApiOperation("查询用户")
    @PostMapping("/selectOne")
    public BaseResponse<User> selectUser(@RequestBody UserSelectDTO userSelectDTO, HttpServletRequest request) {
        if (userSelectDTO == null) {
            throw new BusinessException(StatusCode.NULL_PARAMS);
        }
        User user = new User();
        BeanUtils.copyProperties(userSelectDTO, user);
        User user1 = userService.selectUser(user, request);
        return ResultUtil.success(user1);
    }

    @ApiOperation("查询所有用户")
    @PostMapping("/selectUserList")
    public BaseResponse<List<User>> selectUserList(HttpServletRequest request) {
        List<User> userList = userService.selectAllUser(request);
        return ResultUtil.success(userList);
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public BaseResponse<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("userLoginState");
        return ResultUtil.success("已退出！");
    }

    @ApiOperation("根据用户标签搜索用户")
    @PostMapping("/search/tags")
    public BaseResponse<List<User>> searchUserByTags(@RequestBody List<String> tagNameList) {
        // 为什么service判断了是否为空，这里还要判断？
        // 如果service的方法是第三方或别人写的，防止他没写校验是否为空，导致报错
        if (CollectionUtils.isEmpty(tagNameList)) {
            throw new BusinessException(StatusCode.PARAMS_ERROR);
        }
        List<User> userList = userService.selectUserByTagsToSQL(tagNameList);
        return ResultUtil.success(userList);
    }

    @ApiOperation("获取当前登录用户")
    @GetMapping("/current")
    public BaseResponse<UserLoginVO> getCurrentUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        if (user == null) {
            throw new BusinessException(StatusCode.NO_LOGIN);
        }
        UserLoginVO userLoginVO = new UserLoginVO();
        BeanUtils.copyProperties(user, userLoginVO);
        return ResultUtil.success(userLoginVO);
    }

}

